home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / tchrt3.zip / DELAY.C < prev    next >
Text File  |  1990-07-18  |  5KB  |  137 lines

  1. /*---------------------------------------------------------------------------
  2.  |  Program DELAY.C                                                         |
  3.  |                                                                          |
  4.  |  This program demonstrates the high resolution delay functionality of    |
  5.  |  TCHRT V3.0.                                                             |
  6.  |                                                                          |
  7.  |  (c) 1989 Ryle Design, P.O. Box 22, Mt. Pleasant, Michigan 48804         |
  8.  |                                                                          |
  9.  |  V3.00  Turbo C Shareware Evaluation Version                             |
  10.  ---------------------------------------------------------------------------*/
  11.  
  12. #include <stdio.h>
  13. #include <math.h>
  14.  
  15. #include "pchrt.h"
  16.  
  17. long unsigned       min_delay;
  18. long unsigned       step_delay;
  19. long unsigned       delay_request;
  20. long unsigned       hits;
  21. long unsigned       elapsed;
  22.  
  23. float               delay_error;
  24. float               resolution;
  25.  
  26. tdelay_type         dp;
  27.  
  28.  
  29. void demo_delay(long unsigned how_long)
  30. /*--------------------------------------------------------------------------
  31.  |  This function executes the requested high resolution delay, reports    |
  32.  |  the deviation and percent error, calculates an additional delay        |
  33.  |  optimization, and executes the delay again.                            |
  34.  |                                                                         |
  35.  |  Globals referenced: none                                               |
  36.  |                                                                         |
  37.  |  Arguments: (long unsigned) how_long - delay length in usec             |
  38.  |                                                                         |
  39.  |  Returns  : void                                                        |
  40.  --------------------------------------------------------------------------*/
  41. {
  42.     long unsigned       hits, elapsed;
  43.     float               delay_error, delay_ff;
  44.     tdelay_type         dp;
  45.  
  46.     t_ask_delay(how_long, &dp);
  47.     t_reset(1);
  48.  
  49.     if (delay_request < 54000L)
  50.     {
  51.         printf("Delay with interrupts disabled ... ");
  52.         t_entry(1);
  53.         t_do_delay(&dp);
  54.         t_exit(1);
  55.         printf("complete\n");
  56.     }
  57.     else
  58.     {
  59.         printf("Delay with interrupts enabled ... ");
  60.         t_entry(1);
  61.         t_do_delay_wints(&dp);
  62.         t_exit(1);
  63.         printf("complete\n");
  64.     }
  65.  
  66.     t_ask_timer(1,&hits,&elapsed);
  67.     delay_error = (float) (abs(elapsed - how_long) / (float) how_long) * 100.0;
  68.     printf("Delay requested: %ld  Actual: %ld  Error: %5.3f%%\n\n",how_long,elapsed,delay_error);
  69.  
  70.     printf("Calculating additional delay optimization with t_calc_delay_ff ... ");
  71.     if (how_long < 54000L)
  72.         delay_ff = t_calc_delay_ff(how_long,NO_INTS_ON);
  73.     else
  74.         delay_ff = t_calc_delay_ff(how_long,INTS_ON);
  75.     printf("%6.4f\n",delay_ff);
  76.  
  77.     t_ask_delay(how_long,&dp);
  78.     t_reset(1);
  79.  
  80.     if (delay_request < 54000L)
  81.     {
  82.         printf("Delay with interrupts disabled ... ");
  83.         t_entry(1);
  84.         t_do_delay(&dp);
  85.         t_exit(1);
  86.         printf("complete\n");
  87.     }
  88.     else
  89.     {
  90.         printf("Delay with interrupts enabled ... ");
  91.         t_entry(1);
  92.         t_do_delay_wints(&dp);
  93.         t_exit(1);
  94.         printf("complete\n");
  95.     }
  96.  
  97.     t_ask_timer(1,&hits,&elapsed);
  98.  
  99.     delay_error = (float) (abs(elapsed - how_long) / (float) how_long) * 100.0;
  100.     printf("Delay requested: %ld  Actual: %ld  Error: %5.3f%%\n\n",how_long,elapsed,delay_error);
  101.  
  102. } /* demo_delay */
  103.  
  104.  
  105. void main(void)
  106. {
  107.     printf("Delay Test - TCHRT V3 Demonstration Series\n\n");
  108.  
  109.     t_start();
  110.  
  111.     min_delay = t_min_delay();
  112.     resolution = t_res_delay() / 10.0;
  113.  
  114.     printf("In this hardware environment, minimum delay possible is %ld usec.\n",min_delay);
  115.     printf("Delay resolution is %3.1f usec.\n\n",resolution);
  116.  
  117.     printf("Enter delay in microseconds: %ld - 1000000, 0 to quit >> ",min_delay);
  118.     scanf("%ld",&delay_request);
  119.  
  120.     while (delay_request != 0)
  121.     {
  122.         if (delay_request < min_delay)
  123.             delay_request = min_delay;
  124.         else if (delay_request > 1000000L)
  125.             delay_request = 1000000L;
  126.  
  127.         demo_delay(delay_request);
  128.  
  129.         printf("Enter delay in microseconds: %ld - 1000000, 0 to quit >> ",min_delay);
  130.         scanf("%ld",&delay_request);
  131.     }
  132.  
  133.     t_stop();
  134.     printf("\nDelay Test complete.\n");
  135.  
  136. } /* main */
  137.